#import "scene_game.h"

@implementation SceneGame

@synthesize layerBackground = layerBackground_;
@synthesize layerObject = layerObject_;
@synthesize layerTouch = layerTouch_;
@synthesize layerDialog = layerDialog_;

- (id)initSceneGame
{
	if ((self = [super init]))
	{
		[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];
		
		/*initialization global handles
		*/
		g_SceneGame = self;
		
		/*set up tick update
		*/
		[self schedule:@selector(tick:)];

		/*initializtion physics system
		*/
		CMgrGamePhysics::InitInstance();
		/*initializtion mission system
		*/
		CMgrGameMission::InitInstance();
		/*set up mission
		*/
		CMgrGameMission::Instance()->LoadMission(g_ChapterMissionID);
		CMgrGameMission::Instance()->LoadResource();

		/*layer background
		*/
		layerBackground_ = [[LayerGameBackground alloc] initLayerGameBackground:CMgrGameMission::Instance()->background_type];
		[self addChild:layerBackground_ z:LAYER_GAME_BACKGROUND_Z];
		[layerBackground_ release];
		
		/*layer object
		*/
		layerObject_ = [[LayerGameObject alloc] initLayerGameObject];
		[self addChild:layerObject_ z:LAYER_GAME_OBJECT_Z];
		[layerObject_ release];
		
		/*layer touch
		*/
		layerTouch_ = [[LayerGameTouch alloc] initLayerGameTouch];
		[self addChild:layerTouch_ z:LAYER_GAME_TOUCH_Z];
		[layerTouch_ release];
		
		/*layer dialog
		*/
		layerDialog_ = [[LayerDialog alloc] initLayerDialog];
		[self addChild:layerDialog_ z:LAYER_GAME_DIALOG_Z];
		
#if 0
		/*layer box2d render
		*/
		layerRender_ = [[LayerBox2dRender alloc] initLayerBox2dRender];
		[self addChild:layerRender_ z:LAYER_BOX2D_RENDER_Z];
		[layerRender_ release];
#endif

		CMgrGameMission::Instance()->SetMissionState(MISSION_STATE_SCENE_PREVIEW);
	}

	return self;
}

- (void)dealloc
{
	/*release resource
	*/
	CMgrGameMission::Instance()->FreeResource();

	/*free systems
	*/
	CMgrGamePhysics::FreeInstance();
	CMgrGameMission::FreeInstance();

	/*clear global handles
	*/
	ClearGameHandle();

	[super dealloc];
}

- (void)tick:(ccTime)Time
{
	CMgrGameMission::Instance()->Update();
	CMgrGamePhysics::Instance()->Update();
}

- (void)tickSceneChangeToComic:(ccTime)Time
{
	scene_dummy *_SceneDummy;

	[self unschedule:@selector(tickSceneChangeToComic:)];

	_SceneDummy = [[scene_dummy alloc] initSceneDummy:SCENE_CHANGE_TO_COMIC];
	[[CCDirector sharedDirector] replaceScene:_SceneDummy];
	[_SceneDummy release];
}

- (void)tickSceneChangeToMenu:(ccTime)Time
{
	scene_dummy *_SceneDummy;

	[self unschedule:@selector(tickSceneChangeToMenu:)];

	_SceneDummy = [[scene_dummy alloc] initSceneDummy:SCENE_CHANGE_TO_MENU];
	[[CCDirector sharedDirector] replaceScene:_SceneDummy];
	[_SceneDummy release];
	_SceneDummy.menuState = SCENE_MENU_STATE_MAIN;
}

- (void)tickSceneChangeToChapter:(ccTime)Time
{
	scene_dummy *_SceneDummy;

	[self unschedule:@selector(tickSceneChangeToChapter:)];

	_SceneDummy = [[scene_dummy alloc] initSceneDummy:SCENE_CHANGE_TO_MENU];
	[[CCDirector sharedDirector] replaceScene:_SceneDummy];
	[_SceneDummy release];
	_SceneDummy.menuState = SCENE_MENU_STATE_CHAPTER;
	_SceneDummy.menuChpaterSelectMissionID = menuChapterMissionID_;
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[g_LayerGameObject Pause];
	[g_SpriteGameBow pauseSchedulerAndActions];
	[g_SpriteGameBridge Pause];
	[g_SpriteGameTimeBar Pause];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[g_LayerGameObject Resume];
	[g_SpriteGameBow resumeSchedulerAndActions];
	[g_SpriteGameBridge Resume];
	[g_SpriteGameTimeBar Resume];
}

- (void)FreeLayer
{
	if (nil != g_LayerGameBackground)
	{
		[self removeChild:g_LayerGameBackground cleanup:YES];
		g_LayerGameBackground = nil;
	}
	if (nil != g_LayerGameObject)
	{
		[self removeChild:g_LayerGameObject cleanup:YES];
		g_LayerGameObject = nil;
	}
	if (nil != g_LayerGameMenu)
	{
		[self removeChild:g_LayerGameMenu cleanup:YES];
		g_LayerGameMenu = nil;
	}
	if (nil != g_LayerBox2dRender)
	{
		[self removeChild:g_LayerBox2dRender cleanup:YES];
		g_LayerGameObject = nil;
	}
}

- (void)SceneChangeToComic
{
	sprite_dummy *_Loading;

	/*music
	*/
	MusicStop();

	/*free layers
	*/
	[self FreeLayer];

	/*loading image
	*/
	_Loading = [[sprite_dummy alloc] initSpriteDummy:true];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToComic:) interval:1.0f];
}

- (void)SceneChangeToMenu
{
	sprite_dummy *_Loading;

	/*music
	*/
	MusicStop();

	/*free layers
	*/
	[self FreeLayer];

	/*loading image
	*/
	_Loading = [[sprite_dummy alloc] initSpriteDummy:true];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToMenu:) interval:1.0f];
}

- (void)SceneChangeToChapter:(int)MissionID
{
	sprite_dummy *_Loading;

	/*music
	*/
	MusicStop();

	/*free layers
	*/
	[self FreeLayer];

	/*loading image
	*/
	_Loading = [[sprite_dummy alloc] initSpriteDummy:true];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToChapter:) interval:1.0f];
}

@end